<?php
include( "agent/onlinebet_header.php" );
$sql = "select * from bet where lotto_id=" . $lotto_id . " and rame in (0,9)";
$sql2 = "select * from bet2 where lotto_id=" . $lotto_id . " and rame in (0,9)";
$sql3 = "select * from bet2 where lotto_id=" . $lotto_id . " and rame in (0,9)";
$sql8 = "select * from bet8 where lotto_id=" . $lotto_id . " and rame in (0,9)";
$theacl = $curruser['acl'];
if ($theacl != 255)
{
    $userlist = GetUserList($curruser['id']);
    if ($userlist == "")
    {
        $ui->showErrorPage("<p>还没有建立会员！</p>");
        exit();
    }
    $sql .= " and user_id in (" . $userlist . ")";
    if (2 < $theacl)
    {
        $sql2 .= " and uperid" . $theacl . "=" . $curruser['id'];
    }
    $sql3 .= " and user_id=" . $curruser['id'];
}
if (0 < $disk)
{
    $sql .= " and disk=" . $disk;
}
$percent = 0;
$amounts = array();
$counts = array();
$total = 0;
$sum_count = 0;
$rs = getdb($sql);
while (!$rs->eof)
{
    if ($split == 1)
    {
        if ($theacl == 2)
        {
            $percent = $rs->f['mpercent'];
            if (0 < $percent)
            {
                $percent /= 100;
            }
        }
        else
        {
            if ($theacl == 3)
            {
                $percent = $rs->f['percent2'];
                if (0 < $percent)
                {
                    $percent /= 100;
                }
            }
            else
            {
                if ($theacl == 4)
                {
                    $percent = $rs->f['percent3'];
                    if (0 < $percent)
                    {
                        $percent /= 100;
                    }
                }
                else
                {
                    if ($theacl == 5)
                    {
                        $percent = $rs->f['percent4'];
                        if (0 < $percent)
                        {
                            $percent /= 100;
                        }
                    }
                    else
                    {
                        $percent = $rs->f['upercent'];
                        if (0 < $percent)
                        {
                            $percent /= 100;
                        }
                    }
                }
            }
        }
    }
    else
    {
        $percent = 1;
    }
    $tmp = $percent * $rs->f['amount'];
    $bettype = $rs->f['bettype'];
    if (!isset($amounts[$bettype]))
    {
        $amounts[$bettype] = $tmp;
        $counts[$bettype] = 1;
    }
    else
    {
        $amounts[$bettype] += $tmp;
        ++$counts[$bettype];
    }
    $total += $tmp;
    ++$sum_count;
    $rs->movenext();
}
$rs->close();
if ($disk == 0)
{
    if (2 < $theacl)
    {
        $rs = getdb($sql2);
        while (!$rs->eof)
        {
            $percent = 1;
            if ($theacl == 255)
            {
                $percent = $rs->f['percent255'] - 0;
                if (0 < $percent)
                {
                    $percent /= 100;
                }
            }
            else if ($theacl == 5)
            {
                $percent = $rs->f['percent5'];
                if (0 < $percent)
                {
                    $percent /= 100;
                }
            }
            else if ($theacl == 4)
            {
                $percent = $rs->f['percent4'];
                if (0 < $percent)
                {
                    $percent /= 100;
                }
            }
            else if ($theacl == 3)
            {
                $percent = $rs->f['percent3'];
                if (0 < $percent)
                {
                    $percent /= 100;
                }
            }
            if ($rs->f['prev_id'] == 0 && $theacl != 255)
            {
                $percent = 0;
            }
            $tmp = $percent * $rs->f['amount'];
            $bettype = $rs->f['bettype'];
            if (!isset($amounts[$bettype]))
            {
                $amounts[$bettype] = $tmp;
                $counts[$bettype] = 1;
            }
            else
            {
                $amounts[$bettype] += $tmp;
                ++$counts[$bettype];
            }
            $total += $tmp;
            ++$sum_count;
            $rs->movenext();
        }
        $rs->close();
    }
    if ($theacl == 255)
    {
        $rs = getdb($sql8);
        while (!$rs->eof)
        {
            $tmp = $rs->f['amount'];
            $bettype = $rs->f['bettype'];
            if (isset($amounts[$bettype]))
            {
                $amounts[$bettype] -= $tmp;
            }
            if (isset($counts[$bettype]))
            {
                ++$counts[$bettype];
            }
            $total -= $tmp;
            ++$sum_count;
            $rs->movenext();
        }
    }
    else
    {
        $rs = getdb($sql3);
        while (!$rs->eof)
        {
            $tmp = $rs->f['amount'];
            $bettype = $rs->f['bettype'];
            $amounts[$bettype] -= $tmp;
            ++$counts[$bettype];
            $total -= $tmp;
            ++$sum_count;
            $rs->movenext();
        }
    }
    $rs->close();
}
$dt = $open;
$list = "";
foreach ($g_betname as $k => $value)
{
    $list .= "<tr><td class=td15>" . $g_betname[$k] . "</td><td class=td2 align=right>";
    if (isset($amounts[$k]))
    {
        switch ($k)
        {
            case 0 :
            case 24 :
                $list .= "<a href=\"op.php?op=agent&fp=betlist&dt1=" . $dt . "&dt2=" . $dt . "&bettype=" . $k . "\">" . $counts[$k] . "</a>";
                break;
            case 1 :
            case 2 :
            case 3 :
            case 13 :
            case 14 :
            case 18 :
            case 19 :
            case 20 :
            case 28 :
            case 30 :
                $list .= "<a href=\"op.php?op=agent&fp=betlist1&dt1=" . $dt . "&dt2=" . $dt . "&bettype=" . $k . "&page=-1&order=1\">" . $counts[$k] . "</a>";
                break;
            case 4 :
            case 31 :
                $list .= "<a href=\"op.php?op=agent&fp=betlist&dt1=" . $dt . "&dt2=" . $dt . "&bettype=" . $k . "\">" . $counts[$k] . "</a>";
                break;
            case 5 :
            case 6 :
            case 12 :
            case 15 :
            case 16 :
            case 17 :
            case 23 :
                $list .= "<a href=\"op.php?op=agent&fp=betlist1&dt1=" . $dt . "&dt2=" . $dt . "&bettype=" . $k . "&page=-1&order=1\">" . $counts[$k] . "</a>";
                break;
            case 21 :
            case 22 :
                $list .= "<a href=\"op.php?op=agent&fp=betlist1&dt1=" . $dt . "&dt2=" . $dt . "&bettype=" . $k . "&page=-1&order=1\">" . $counts[$k] . "</a>";
                break;
            case 25 :
            case 26 :
                $list .= "<a href=\"op.php?op=agent&fp=betlist&dt1=" . $dt . "&dt2=" . $dt . "&bettype=" . $k . "\">" . $counts[$k] . "</a>";
                break;
            default :
                $list .= "<a href=\"op.php?op=agent&fp=betlist1&dt1=" . $dt . "&dt2=" . $dt . "&bettype=" . $k . "&page=-1&order=0\">" . $counts[$k] . "</a>";
                break;
        }
        $list .= "</td><td class=td2 align=right>" . round($amounts[$k]);
    }
    else
    {
        $list .= "0</td><td class=td2 align=right>0";
    }
    $list .= "</td></tr>";
}
$tpl->set("list", $list);
$tpl->set("total", round($total));
$tpl->set("sum_count", $sum_count);
$tpl->load("main", "onlinebet1.htm");
$tpl->show("main");
$ui->footer();